<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 9/27/14
 * Time: 2:38 PM
 */

namespace files;

use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

if (!defined('NYMPH_ROOT')) {
    define('NYMPH_ROOT', dirname("../../../" . __FILE__));
    require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}

class FileUtils implements IFileUtils
{

    /**
     * (PHP 5)<br/>
     * Change path slashes to system dependent
     * @param string $path
     * @return string string with changed slashes
     */
    static function convertToSystemDependentPath($path)
    {
        switch (DIRECTORY_SEPARATOR):
            case '/': {
                $path = str_replace('\\', '/', $path);
                break;
            }
            case '\\': {
                $path = str_replace('/', '\\', $path);
                break;
            }
        endswitch;
        return $path;
    }

    /**
     * (PHP 5)<br/>
     * Get relative path. Finds first $rel param
     * @param string $path
     * @param string $rel
     * @param boolean $convert [optional] convert path to system dependent
     * @return string relative path
     */
    static function getRelativePath($path, $rel, $convert)
    {
        if (strpos($path, $rel)) {
            if ($convert) {
                $path = FileUtils::convertToSystemDependentPath(substr($path, strpos($path, $rel) + strlen($rel)));
            } else {
                $path = substr($path, strpos($path, $rel) + strlen($rel));
            }
        } else {
            if ($convert) {
                $path = FileUtils::convertToSystemDependentPath($path);
            }
        }
        return $path;
    }

    /**
     * (PHP 5)<br/>
     * Get relative path. Finds last $rel param
     * @param string $path
     * @param string $rel
     * @param boolean $convert [optional] convert path to system dependent
     * @return string relative path
     */
    static function getRelativePathFromRight($path, $rel, $convert)
    {
        if (strrpos($path, $rel)) {
            if ($convert) {
                $path = FileUtils::convertToSystemDependentPath(substr($path, strrpos($path, $rel) + strlen($rel)));
            } else {
                $path = substr($path, strrpos($path, $rel) + strlen($rel));
            }
        } else {
            if ($convert) {
                $path = FileUtils::convertToSystemDependentPath($path);
            }
        }
        return $path;
    }

    /**
     * (PHP 5)<br/>
     * Get file extension
     * @param string $file
     * @return string relative path or false if file has no extension
     */
    static function getFileExtension($file)
    {
        return substr($file, strrpos($file, '.') + 1);
    }

    /**
     * (PHP 5)<br/>
     * Writing to file
     * @param string $file
     * @param string $subject
     * @param boolean $overwrite [optional] default = false
     * @return boolean
     */
    static function writeToFile($file, $subject, $overwrite)
    {
        if (self::checkAndCreateFile($file)) {
            if ($overwrite) {
                $file = fopen($file, "w");
                $check = fwrite($file, $subject);
                if ($check && fclose($file)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return FileUtils::appendFile($file, $subject);
            }
        } else {
            return false;
        }
    }

    /**
     * (PHP 5)<br/>
     * Writing to the end of file file. If cannot write returns false.
     * @param string $file
     * @param string $subject
     * @return boolean
     */
    static function appendFile($file, $subject)
    {
        $file = fopen($file, "a");
        $check = fwrite($file, $subject);
        if ($check && fclose($file)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * (PHP 5)<br/>
     * Checking file existence. If file does not exist creating file with
     * directory (if it was wrote).
     * @param string $file
     * @return boolean
     */
    static function checkAndCreateFile($file)
    {
        if (!file_exists($file)) {
            mkdir(self::getDirs($file), 0777, true);
            $fileHandle = fopen($file, 'w');
            fclose($fileHandle);
            if ($fileHandle) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * (PHP 5)<br/>
     * Returns path to file< without this file name.
     * Can be used to creating paretn dirs for the specified file
     * @param string $path
     * @return string path
     */
    static function getDirs($path)
    {
        return dirname($path);
    }

    /**
     * (PHP 5)<br/>
     * Returns array with all files from directory from $dirForScan.
     * @param string $directory
     * @return array
     */
    static function getAllFilesFromDir($directory)
    {

        $objects = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($directory),
            RecursiveIteratorIterator::SELF_FIRST);
        return $objects;
    }

    /**
     * (PHP 5)<br/>
     * Return path of file.
     * @param string $pathToFile
     * @return string
     */
    static function getFilePath($pathToFile)
    {
        return basename($pathToFile);
    }
}
